name: Check broken links on Saturday

on:
  schedule:
    - cron:  '00 5 * * SAT'
  workflow_dispatch:
jobs:
  check-links:
    name: Check broken links
    runs-on: ubuntu-latest
    needs:
      - combine-site
    steps:
      - uses: actions/download-artifact@v1
        with:
          name: vuepress-site

      - name: Check links
        uses: chabad360/htmlproofer@master
        with:
          directory: vuepress-site
          arguments: >-
            --assume-extension
            --typhoeus-config '{"timeout": 60}'
            --ignore-urls "/www\.consultant\.ru/.*/,/github.com\/.*\/edit\/.*/"

  combine-site:
    name: Combine artifacts and site
    runs-on: ubuntu-latest
    needs:
      - generate-roadmap-freemind-plantuml
      - generate-roadmap-image
      - build-vuepress
    steps:
      - uses: actions/download-artifact@v1
        with:
          name: vuepress-pre-site

      - uses: actions/download-artifact@v1
        with:
          name: trees

      - uses: actions/download-artifact@v1
        with:
          name: plantuml

      - uses: actions/download-artifact@v1
        with:
          name: freemind

      - uses: actions/download-artifact@v1
        with:
          name: images

      - run: |
          mv -v plantuml/* vuepress-pre-site
          mv -v freemind/* vuepress-pre-site
          mv -v images/* vuepress-pre-site
          mv -v trees/* vuepress-pre-site

      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: vuepress-site
          path: vuepress-pre-site

  build-vuepress:
    name: Build VuePress site and generate roadmap tree
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Setup Node
        uses: actions/setup-node@v1
        with:
          node-version: '16'

      - name: Cache dependencies
        uses: actions/cache@v1
        with:
          path: ~/.cache/yarn
          key: ${{ runner.os }}-yarn-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install js dependencies
        run: yarn install --frozen-lockfile

      - name: Build vuepress site
        run: yarn run site:build

      - name: Upload pre-site to artifact
        uses: actions/upload-artifact@v1
        with:
          name: vuepress-pre-site
          path: tlroadmap/.vuepress/dist

      - name: Upload trees
        uses: actions/upload-artifact@v1
        with:
          name: trees
          path: tlroadmap/.vuepress/trees

  generate-roadmap-freemind-plantuml:
    name: Generate .mm and .puml
    runs-on: ubuntu-latest
    needs:
      - build-vuepress
    steps:
      - uses: actions/checkout@v2

      - uses: actions/download-artifact@v1
        with:
          name: trees

      - name: Set up Python 3.8
        uses: actions/setup-python@v1
        with:
          python-version: 3.8

      - name: Cache dependencies
        uses: actions/cache@v1
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r tools/requirements.txt

      - name: Run plantuml2freemind
        run: |
          mkdir freemind
          mkdir plantuml
          plantuml2freemind convert trees/tree-ru.json freemind/roadmap-ru.mm --no-interaction
          plantuml2freemind convert trees/tree-ru.json plantuml/roadmap-ru.puml --no-interaction

      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: freemind
          path: freemind

      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: plantuml
          path: plantuml

  generate-roadmap-image:
    name: Generate .svg and .png
    runs-on: ubuntu-latest
    needs:
      - generate-roadmap-freemind-plantuml
    steps:
      - uses: actions/checkout@v2

      - uses: actions/download-artifact@v1
        with:
          name: plantuml

      - name: Setup Java
        uses: actions/setup-java@v1
        with:
          java-version: '11'

      - name: Generate .png and .svg
        run: |
          mkdir images
          cat plantuml/roadmap-ru.puml | java -jar tools/plantuml.jar -charset UTF-8 -pipe > images/roadmap-ru.png
          cat plantuml/roadmap-ru.puml | java -jar tools/plantuml.jar -charset UTF-8 -tsvg -pipe > images/roadmap-ru.svg

      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: images
          path: images
